<template>
    <div class="fullScreen bg-c-wh">
        <!-- header、倒计时、玩法提示等 -->
        <lot-header></lot-header>
        <div class="scroll-lot tx-s-14" v-if="lotData != null">
            <!-- 特码 -->
            <div v-show="lotIndex == 0">
                <lhc-options :title="['tm', '']" :content="c1z49Cont" :odds="lotOdds.tm.pl" :temp="7"></lhc-options>
                <lhc-options :title="['tm', '']" :content="hwbCont" :odds="lotOdds.tm" :temp="4"></lhc-options>
            </div>
            <!-- 正码 -->
            <div v-show="lotIndex == 1">
                <lhc-options :title="['zm', '']" :content="c1z49Cont" :odds="lotOdds.zm.pl" :temp="7"></lhc-options>
                <lhc-options :title="['zm', '']" :content="zdxdsCont" :odds="lotOdds.zm" :temp="4"></lhc-options>
            </div>
            <!-- 正特码 -->
            <div v-show="lotIndex == 2">
                <lhc-cont-tab :title="'ztm'" :tab="ztmPlay"></lhc-cont-tab>
            </div>
            <!-- 连码 -->
            <div v-show="lotIndex == 3">
                <lhc-cont-tab :title="'lm'" :tab="lmPlay"></lhc-cont-tab>
            </div>
            <!-- 过关 -->
            <div v-show="lotIndex == 4">
                <div v-for="(value, key) in ggPlay" :key="key">
                    <div class="title-lot">{{ value }}</div>
                    <lhc-options :title="[key, value]" :content="ggCont" :odds="lotOdds.gg" :temp="4"></lhc-options>
                </div>
            </div>
            <!-- 特肖 -->
            <div v-show="lotIndex == 5" v-if="this.$store.state.sxNumber != ''">
                <lhc-options :title="['tx', '']" :content="sxCont()" :odds="lotOdds.tx" :temp="2"></lhc-options>
            </div>
            <!-- 特码头尾 -->
            <div v-show="lotIndex == 6">
                <div class="title-lot">特玛头</div>
                <lhc-options :title="['tmtw', '']" :content="tmtCont" :odds="lotOdds.tmtw.tmt" :temp="4"></lhc-options>
                <div class="title-lot">特玛尾</div>
                <lhc-options :title="['tmtw', '']" :content="tmwCont" :odds="lotOdds.tmtw.tmw" :temp="4"></lhc-options>
            </div>
            <!-- 五行 -->
            <div v-show="lotIndex == 7">
                <lhc-options :title="['wx', '']" :content="wxCont" :odds="lotOdds.wx" :temp="1"></lhc-options>
            </div>
            <!-- 半波 -->
            <div v-show="lotIndex == 8">
                <lhc-options :title="['bb', '']" :content="bbCont" :odds="lotOdds.bb" :temp="2"></lhc-options>
            </div>
            <!-- 七码 -->
            <div v-show="lotIndex == 9">
                <div v-for="(value, key) in qmPlay" :key="key">
                    <div class="title-lot">{{ value }}</div>
                    <lhc-options :title="['qm', '']" :content="qmCont[key]" :odds="lotOdds.qm" :temp="4"></lhc-options>
                </div>
            </div>
            <!-- 六肖 -->
            <div v-show="lotIndex == 10">
                <lhc-cont-tab :title="'lx'" :tab="lxPlay"></lhc-cont-tab>
            </div>
            <!-- 一肖尾数 -->
            <div v-show="lotIndex == 11">
                <lhc-cont-tab :title="'yxws'" :tab="yxwsPlay"></lhc-cont-tab>
            </div>
            <!-- 生肖连 -->
            <div v-show="lotIndex == 12">
                <lhc-cont-tab :title="'sxl'" :tab="sxlPlay"></lhc-cont-tab>
            </div>
            <!-- 尾数连 -->
            <div v-show="lotIndex == 13">
                <lhc-cont-tab :title="'wsl'" :tab="wslPlay"></lhc-cont-tab>
            </div>
            <!-- 不中 -->
            <div v-show="lotIndex == 14">
                <lhc-cont-tab :title="'bz'" :tab="bzPlay"></lhc-cont-tab>
            </div>
            <!-- 多选中一 -->
            <div v-show="lotIndex == 15">
                <lhc-cont-tab :title="'dxzy'" :tab="dxzyPlay"></lhc-cont-tab>
            </div>
            <!-- 特平中 -->
            <div v-show="lotIndex == 16">
                <lhc-cont-tab :title="'tpz'" :tab="tpzPlay"></lhc-cont-tab>
            </div>
        </div>
        <!-- footer -->
        <div class="flex-box flex-pj lotteryFooter tx-s-14">
            <div class="tx-c-r bor-r-g footer-btn" @click="cancel">重置</div>
            <div class="flex-box flex-f1">
                <input v-model="money" class="input-ol-n wd-fh tx-c" type="number" placeholder="请输入单注金额" />
            </div>
            <div class="tx-c-g bor-l-g footer-btn" @click="confirm">确定</div>
        </div> 
    </div>
</template>
<script type="text/ecmascript-6">
import 'common/css/lottery.css'
import { mapGetters } from 'vuex' 
import Trans from '../../../common/js/eventTrans'
import LotHeader from "../../../components/lottery/lot-header"
import LhcContTab from "../../../components/lottery/lhcContTab"
import LhcOptions from "../../../components/lottery/lhcOptions"
import { isEmptyObject, openLotOrder } from '../../../common/js/common'
import { ztmPlay, ggPlay, qmPlay, lxPlay, yxwsPlay, sxlPlay, wslPlay, bzPlay, dxzyPlay, tpzPlay, lmPlay } from '../../../common/js/data'
import { c1z49Cont, hwbCont, zdxdsCont, tmtCont, tmwCont, ggCont, wxCont, bbCont, qmdanCont, qmsCont, qmdaCont, qmxCont, sxCont } from '../../../common/js/data'
import { sxNumber } from '../../../common/js/axiosUtil'
export default {
    data () {
        return {
            allResult : [],
            money : '',
            c1z49Cont : c1z49Cont(), // 数字内容
            hwbCont : hwbCont, // 特码合尾波内容
            zdxdsCont : zdxdsCont, // 正码总内容
            ztmPlay : ztmPlay, // 正特码玩法
            lmPlay : lmPlay, // 连码玩法
            ggPlay : ggPlay, // 过关玩法
            ggCont : ggCont, // 过关内容
            tmtCont : tmtCont, // 特玛头内容
            tmwCont : tmwCont, // 特玛尾内容
            wxCont : wxCont, // 五行内容
            bbCont : bbCont, // 半波内容
            qmPlay : qmPlay, // 七码玩法
            qmCont : {
                qmdanCont : qmdanCont, // 七码单内容
                qmsCont : qmsCont, // 七码双内容
                qmdaCont : qmdaCont, // 七码大内容
                qmxCont : qmxCont // 七码小内容
            },
            lxPlay : lxPlay, // 六肖玩法
            yxwsPlay : yxwsPlay, // 一肖尾数玩法 
            sxlPlay : sxlPlay, // 生肖连玩法
            wslPlay : wslPlay, //尾数连玩法
            bzPlay : bzPlay, // 不中玩法
            dxzyPlay : dxzyPlay, // 多选中一玩法
            tpzPlay : tpzPlay, // 特平中玩法
        }
    },
    created () {
        this.getSxNumber()
    },
    mounted () {
        var _this = this
        Trans.$on('togLotteryTab', function (msg) {
            _this.cancel()
        })
    },
    methods: {
        // 重置下注数据
        cancel () { 
            this.money = ''
            this.$store.state.lotOptTra = []
            this.$store.state.lotOptions = []
            this.$store.state.lotSelData = []
        },
        // 确定
        confirm () {
            const {lotStatus, lotSelData, lhcTra, lotTitle, lhcPlay} = this.$store.state
            if(lotStatus == 0){
                this.$alert('已封盘')
            }else if(lotStatus == -1){ 
                this.$alert('已关盘')
            }else if(isEmptyObject(lotSelData)){
                this.$toast('未下注', 2000)
            }else if(lotStatus == 1){
                if(this.lotIndex == 4){
                    this.ggData(lotSelData)
                }else if(lotTitle ==  '连码' && lhcTra > 1){
                    this.lmData(lotSelData)
                }else if(this.$store.state.lhcPlay > -1){
                    if(lhcTra == -1){
                        this.permutation(lotSelData)
                    }else{
                        this.permutationTra(lotSelData)
                    }
                }else{
                    openLotOrder(this.money)
                }
            }
        },
        lmData (data) {
            let arrs = [[], []]
            // 选中数据按分组
            for(let i=0; i<data.length; i++){
                const play = data[i].playType.split('_')
                const playType = play[0]+'_'+play[1]+'_'+play[2]
                const key = play[3].slice(2)
                const name = data[i].name
                let cont = data[i].contN
                let selObj = {}
                if(this.$store.state.lhcTra == 5){
                    cont = key == 0 ? 'A' : 'B'
                    selObj = {
                        'playType' : playType,
                        'content' : name,
                        'odds' : data[i].odds,
                        'name' :  cont + name,
                        'nameT' : data[i].nameT
                    }
                    arrs[key].push(selObj)
                }else{
                    for(let x=0; x<cont.length; x++){
                        selObj = {
                            'playType' : playType,
                            'content' : cont[x],
                            'odds' : data[i].odds,
                            'name' :  cont[x],
                            'nameT' : data[i].nameT
                        }
                        arrs[key].push(selObj)
                    }
                }
            }
            if(arrs[0].length > 0 && arrs[1].length > 0){
                this.doExchange(arrs)
            }else{
                this.$toast('所选内容未构成一注', 2000)
            }
        },
        ggData (data) {
            let gg = {zm1 : [], zm2 : [], zm3 : [], zm4 : [], zm5 : [], zm6 : []}
            let type = [], arrs = []
            // 选中数据按照正码1-6分组
            for(let i=0; i<data.length; i++){
                const key = data[i].playType.substr(0, 3)
                const selObj = {
                    'content' : data[i].playType,
                    'odds' : data[i].odds,
                    'name' :  data[i].name
                }
                gg[key].push(selObj)
                if(type.indexOf(key)<0){
                    type.push(key)
                }
            }
            // 转化为二维数组
            if(type.length > 1){
                for(let i=0; i<type.length; i++){
                    arrs[i] = gg[type[i]]
                }
                this.doExchange(arrs)
            }else{
                this.$toast('所选内容未构成一注', 2000)
            }
        },
        doExchange(douArr) {
            // douArr 二维数组 fgf 分隔符
            let len = douArr.length
            if (len > 1) {
                let len1 = douArr[0].length
                let len2 = douArr[1].length
                let newlen = len1 * len2
                let temp = new Array(newlen)
                let index = 0
                for (let i = 0; i < len1; i++) {
                    for (let j = 0; j < len2; j++) {
                        const d0 = douArr[0][i]
                        const d1 = douArr[1][j]
                        const cont = d0.content + ',' + d1.content
                        const name = d0.name + ',' + d1.name
                        let play = '', odds = '', nameT = ''
                        if(this.lotIndex == 4){
                            play = 'gg'
                            odds = d0.odds * d1.odds
                        }else{
                            play = d0.playType
                            odds = d0.odds
                            nameT = d0.nameT
                        }
                        const selObj = {
                            'playType' : play,
                            'content' : cont,
                            'odds' : odds,
                            'name' :  name,
                            'nameT' : nameT,
                            'issue' : this.lotData.countDown.issue
                        }
                        temp[index] = selObj
                        index ++
                    }
                }
                let newArray = new Array(len - 1)
                for (let i = 2; i < len; i++) {
                    newArray[i - 1] = douArr[i]
                }
                newArray[0] = temp
                return this.doExchange(newArray)
            }else{
                if(this.lotIndex == 3){
                    for (var i = 0; i < douArr[0].length; i++) {
                        const name = douArr[0][i].name.split(',')
                        if(name[0] == name[1]) douArr[0].splice(i, 1)
                    }
                }else if(this.lotIndex == 4){
                    for (let i = 0; i < douArr[0].length; i++) {
                        douArr[0][i].odds = douArr[0][i].odds.toFixed(2)
                    }
                }
                this.$store.state.lotSelData = douArr[0]
                openLotOrder(this.money)
            }
        },
        // 普通多选、复式数据重组
        permutation (data) {
            this.allResult = []
            const { lhcPlay } = this.$store.state
            if(data.length >= lhcPlay){
                this.combination(data, lhcPlay, [])
                this.$store.state.lotSelData = this.allResult
                openLotOrder(this.money)
            }else{
                this.$toast('所选内容未构成一注', 2000)
            }
        },
        // 递归组合数据
        combination (arr, size, result) {
            const arrLen = arr.length
            let selObj = ''
            if (size == arrLen) {
                const all = [].concat(result, arr)
                selObj = this.reData(all)
                this.allResult.push(selObj)
            } else {
                for (let i = 0 ; i < arrLen; i++) {
                    let newResult = [].concat(result)
                    newResult.push(arr[i])
                    if (size == 1) {
                        selObj = ''
                        selObj = this.reData(newResult)
                        this.allResult.push(selObj)
                    } else {
                        let newArr = [].concat(arr)
                        newArr.splice(0, i + 1)
                        this.combination(newArr, size - 1, newResult)
                    }
                }
            }
        },
        // 拼接递归数据
        reData (data) {
            let cont = data[0].content
            let name = data[0].name
            let odds = data[0].odds
            let selObj = ''
            const { lhcPlay } = this.$store.state
            for(let i = 0; i < data.length - 1; i++){
                let n = data[i+1]
                if(odds.indexOf('/') < 0 && n.odds < odds){
                    odds = n.odds
                }
                cont += ',' + n.content
                name += ',' + n.name
            }
            selObj = {
                'playType' : data[0].playType,
                'content' : cont,
                'odds' : odds,
                'name' : name,
                'nameT' : data[0].nameT,
                'issue' : this.lotData.countDown.issue
            }
            return selObj
        },
        // 拖头数据重组
        permutationTra (data) {
            const { lhcPlay } = this.$store.state
            const tra = lhcPlay-1
            if(data.length >= lhcPlay){
                let reData = []
                // 拼接拖头
                const tArr = data.slice(0, tra)
                const tData = this.reData(tArr)
                // 拼接尾巴
                const wArr = data.slice(tra)
                for(let i=0; i<wArr.length; i++){
                    const newArr = [].concat(tData, wArr[i])
                    reData.push(this.reData(newArr))
                }
                this.$store.state.lotSelData = reData
                openLotOrder(this.money)
            }else{
                this.$toast('所选内容未构成一注', 2000)
            }
        },
        // 获取彩票数据
        getSxNumber () {
            let params = this.$params(0)
            sxNumber(params)
            .then((response)=> {
                if(response.data.code == "200"){ 
                    let data = response.data.data.sxNumber
                    this.$store.state.sxNumber = data
                } else {
                    this.$logAgain(response.data.msg)
                }
            })
            .catch((error)=> {
                this.$toast('请求服务失败', 2000)
            })
        },
        // 打开玩法提示
        showDesc () {
            this.$store.state.lotDesc = true
        },
        sxCont () {
            return sxCont(1, '')
        }
    },
    components: {
        LotHeader,
        LhcContTab,
        LhcOptions
    },
    computed: {
        ...mapGetters([
            'lotData',
            'lotOdds',
            'lotIndex'
        ])
    }
};
</script>